package com.zhengshang.api.service.impl;

import java.util.List;

import com.zhengshang.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhengshang.api.mapper.AppSigninRecordMapper;
import com.zhengshang.api.domain.AppSigninRecord;
import com.zhengshang.api.service.IAppSigninRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户签到记录Service业务层处理
 *
 * @author zhengshang
 * @date 2021-03-10
 */
@Service
public class AppSigninRecordServiceImpl implements IAppSigninRecordService {
    @Autowired
    private AppSigninRecordMapper appSigninRecordMapper;

    /**
     * 查询用户签到记录
     *
     * @param id 用户签到记录ID
     * @return 用户签到记录
     */
    @Override
    public AppSigninRecord selectAppSigninRecordById(Long id) {
        return appSigninRecordMapper.selectAppSigninRecordById(id);
    }

    /**
     * 查询用户签到记录列表
     *
     * @param appSigninRecord 用户签到记录
     * @return 用户签到记录
     */
    @Override
    public List<AppSigninRecord> selectAppSigninRecordList(AppSigninRecord appSigninRecord) {
        return appSigninRecordMapper.selectAppSigninRecordList(appSigninRecord);
    }

    @Override
    public AppSigninRecord getUpperAppSigninRecord(Long userId, String signinDate) {
        AppSigninRecord list = appSigninRecordMapper.getUpperAppSigninRecord(userId, signinDate);
        if (ObjectUtils.isNotEmpty(list)) {
            return list;
        } else {
            return null;
        }
    }

    /**
     * 新增用户签到记录
     *
     * @param appSigninRecord 用户签到记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAppSigninRecord(AppSigninRecord appSigninRecord) {
        AppSigninRecord upperRecord = this.getUpperAppSigninRecord(appSigninRecord.getUserId(), DateUtils.dateCalculationDay(-1, "yyyyMMdd"));
        if (upperRecord != null) {
            appSigninRecord.setSigninContinuityDays(upperRecord.getSigninContinuityDays() + 1);
        } else {
            appSigninRecord.setSigninContinuityDays(1);
        }
        appSigninRecord.setCreateTime(DateUtils.getNowDate());
        return appSigninRecordMapper.insertAppSigninRecord(appSigninRecord);
    }

    /**
     * 修改用户签到记录
     *
     * @param appSigninRecord 用户签到记录
     * @return 结果
     */
    @Override
    public int updateAppSigninRecord(AppSigninRecord appSigninRecord) {
        appSigninRecord.setUpdateTime(DateUtils.getNowDate());
        return appSigninRecordMapper.updateAppSigninRecord(appSigninRecord);
    }

    /**
     * 批量删除用户签到记录
     *
     * @param ids 需要删除的用户签到记录ID
     * @return 结果
     */
    @Override
    public int deleteAppSigninRecordByIds(Long[] ids) {
        return appSigninRecordMapper.deleteAppSigninRecordByIds(ids);
    }

    /**
     * 删除用户签到记录信息
     *
     * @param id 用户签到记录ID
     * @return 结果
     */
    @Override
    public int deleteAppSigninRecordById(Long id) {
        return appSigninRecordMapper.deleteAppSigninRecordById(id);
    }
}